package com.craftplay.dao;

import com.craftplay.model.Enroll;
import com.craftplay.model.Unit;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface EnrollMapper {

    @Insert({
        "insert into enroll (unit_id, ",
        "owner_id, actor_id, descrip, status, created_time)",
        "values (#{unitId,jdbcType=INTEGER},",
        "#{ownerId,jdbcType=INTEGER}, #{actorId,jdbcType=INTEGER},#{descrip,jdbcType=VARCHAR},",
        "#{status,jdbcType=INTEGER}, #{createdTime,jdbcType=TIMESTAMP})"
    })
    int insert(Enroll enroll);



    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, descrip, status, created_time",
            "from enroll",
            "where unit_id = #{unitId,jdbcType=INTEGER} and status != -1 and status != 1"
    })
    List<Enroll> selectByUnitId(int unitId);


    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, descrip, status, created_time",
            "from enroll",
            "where unit_id = #{unitId,jdbcType=INTEGER}"
    })
    List<Enroll> selectAllUnitByUnitId(int unitId);


    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, descrip, status, created_time",
            "from enroll",
            "where actor_id = #{actorId,jdbcType=INTEGER} and status != 1"
    })
    List<Enroll> selectReqEnrollByActorId(int actorId);


    //Select all passed activity by actorId
    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, descrip, status, created_time",
            "from enroll",
            "where actor_id = #{actorId,jdbcType=INTEGER} and status = 1"
    })
    List<Enroll> selectAllPassedByActorId(int actorId);



    @Select({
            "select",
            "*",
            "from enroll",
            "where unit_id = #{unitId,jdbcType=INTEGER}"
    })
    List<Enroll> getStatisticByUnitId(int unitId);



    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.INTEGER, id=true),
            @Result(column="unit_id", property="unitId", jdbcType=JdbcType.INTEGER),
            @Result(column="owner_id", property="ownerId", jdbcType=JdbcType.INTEGER),
            @Result(column="actor_id", property="actorId", jdbcType=JdbcType.VARCHAR),
            @Result(column="descrip", property="descrip", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="created_time", property="createdTime", jdbcType=JdbcType.TIMESTAMP)
    })



    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, descrip, status, created_time",
            "from enroll",
            "where actor_id = #{actorId,jdbcType=INTEGER} and unit_id = #{unitId,jdbcType=INTEGER} and status != -1"
    })
    List<Enroll> selectByActorIdAndUnitId(@Param("actorId") int actorId, @Param("unitId") int unitId);


    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.INTEGER, id=true),
            @Result(column="unit_id", property="unitId", jdbcType=JdbcType.INTEGER),
            @Result(column="owner_id", property="ownerId", jdbcType=JdbcType.INTEGER),
            @Result(column="actor_id", property="actorId", jdbcType=JdbcType.VARCHAR),
            @Result(column="descrip", property="descrip", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="created_time", property="createdTime", jdbcType=JdbcType.TIMESTAMP)
    })

    @Select({
            "select",
            "id, unit_id, owner_id, actor_id, descrip, status, created_time",
            "from enroll",
            "where unit_id = #{unitId,jdbcType=INTEGER} and status = 1"
    })

    List<Enroll> getPassedEnrollByUnitId(@Param("unitId") int unitId);

    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.INTEGER, id=true),
            @Result(column="unit_id", property="unitId", jdbcType=JdbcType.INTEGER),
            @Result(column="owner_id", property="ownerId", jdbcType=JdbcType.INTEGER),
            @Result(column="actor_id", property="actorId", jdbcType=JdbcType.VARCHAR),
            @Result(column="descrip", property="descrip", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="created_time", property="createdTime", jdbcType=JdbcType.TIMESTAMP)
    })

//  @Select({
//            "select",
//            "",
//            "from enroll",
//            "where actor_id = #{actorId,jdbcType=INTEGER}"
// })
// int selectByActorId(int actorId);

    @Update({
        "update enroll",
        "set status = #{status, jdbcType=INTEGER}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateStatus(@Param("id") int id,@Param("status") int status);

    @Select({
            "select",
            "*",
            "from enroll",
            "where id = #{id,jdbcType=INTEGER}"
    })
    Enroll getEnrollById(@Param("id") int id);
}
